odoo.define('web_optimize.HierarchyModel', function (require) {
"use strict";

var AbstractModel = require('web.AbstractModel');


return AbstractModel.extend({
    /**
     * @override
     * @param {Widget} parent
     */
    init: function (parent, params) {
        this._super.apply(this, arguments);
        this.data = undefined;
        this.hierarchy = {};
    },
    get: function () {
        return this.data;
    },
    load: function (params) {
        this.modelName = params.modelName;
        this.hierarchy = Object.assign({
            domain: params.domain
        });
        return this._fetchData();
    },
    reload: function (handle, params) {
        if ('domain' in params) {
            this.hierarchy.domain = params.domain;
        }
        return this._fetchData();
    },
    _fetchData: function () {
        var self = this;
        return this._rpc({
            model: this.modelName,
            method: 'get_hierarchy_data',
            kwargs: {
                domain: this.hierarchy.domain
            }
        }).then(function (res) {
            self.data = res;
        })
    }
});

});
